package com.sgb.trip.view.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.databinding.DataBindingUtil;

import com.luck.picture.lib.tools.AnimUtils;
import com.sgb.link.entity.trip.AirportCityEntity;
import com.sgb.link.entity.trip.FlightSearchEntity;
import com.sgb.trip.R;
import com.sgb.trip.databinding.TripPopwindowFlightSingleBinding;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MToast;

import java.text.SimpleDateFormat;
import java.util.Date;

public class FlightPopWindow extends LinearLayout implements View.OnClickListener {

    protected OnClickCallBack onClickCallBack;
    protected OnSearchCallBack onSearchCallBack;
    TripPopwindowFlightSingleBinding binding;
    FlightSearchEntity mEntity;

    public FlightPopWindow(Context context, FlightSearchEntity entity) {
        super(context);
        this.mEntity = new FlightSearchEntity();
        mEntity.setBack(entity.isBack());
        mEntity.setSingle(entity.isSingle());
        mEntity.setStartCity(entity.getStartCity());
        mEntity.setStartCityNo(entity.getStartCityNo());
        mEntity.setArriveCity(entity.getArriveCity());
        mEntity.setArriveCityNo(entity.getArriveCityNo());
        mEntity.setStartDate(entity.getStartDate());
        mEntity.setStartDateStamp(entity.getStartDateStamp());
        mEntity.setReturnDate(entity.getReturnDate());
        mEntity.setReturnDateStamp(entity.getReturnDateStamp());
        mEntity.setStartWeek(entity.getStartWeek());
        mEntity.setBackWeek(entity.getBackWeek());
        init(context, null);
    }

    public FlightPopWindow(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public FlightPopWindow(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.trip_popwindow_flight_single, this, true);
        binding.setOnClick(this);

        binding.tvChooseCityStart.setText(mEntity.getStartCity());
        binding.tvChooseCityStart.setTextColor(getResources().getColor(R.color.black));

        binding.tvChooseCityArrive.setText(mEntity.getArriveCity());
        binding.tvChooseCityArrive.setTextColor(getResources().getColor(R.color.black));

        binding.tvChooseDateStart.setText(mEntity.getStartDate());
        binding.tvChooseDateStart.setTextColor(getResources().getColor(R.color.black));

        if (!mEntity.isSingle()) {
            binding.tvChooseDateArrive.setVisibility(VISIBLE);
            binding.tvChooseDateArrive.setText(mEntity.getReturnDate());
            binding.tvChooseDateArrive.setTextColor(getResources().getColor(R.color.black));
        }
    }

    /**
     * 填充出发城市
     *
     * @param aMapAddressEntity
     */
    public void setStartCity(AirportCityEntity aMapAddressEntity) {
        if (!TextUtils.isEmpty(mEntity.getArriveCityNo()) && mEntity.getArriveCityNo().equals(aMapAddressEntity.getCityCode())) {
            MToast.showToast(getContext(), "不能选择相同城市");
            return;
        }
        binding.tvChooseCityStart.setText(aMapAddressEntity.getCityName());
        binding.tvChooseCityStart.setTextColor(getResources().getColor(R.color.black));
        mEntity.setStartCity(aMapAddressEntity.getCityName());
        mEntity.setStartCityNo(aMapAddressEntity.getCityCode());
    }

    /**
     * 填充到达城市
     *
     * @param aMapAddressEntity
     */
    public void setArriveCity(AirportCityEntity aMapAddressEntity) {
        if (!TextUtils.isEmpty(mEntity.getStartCityNo()) && mEntity.getStartCityNo().equals(aMapAddressEntity.getCityCode())) {
            MToast.showToast(getContext(), "不能选择相同城市");
            return;
        }
        binding.tvChooseCityArrive.setText(aMapAddressEntity.getCityName());
        binding.tvChooseCityArrive.setTextColor(getResources().getColor(R.color.black));
        mEntity.setArriveCity(aMapAddressEntity.getCityName());
        mEntity.setArriveCityNo(aMapAddressEntity.getCityCode());
    }

    /**
     * 填充出发日期
     *
     * @param date
     */
    public void setStartDate(Date date) {
        String dateFormat = DateUtils.timestampToDate(date.getTime());
        binding.tvChooseDateStart.setText(dateFormat);
        binding.tvChooseDateStart.setTextColor(getResources().getColor(R.color.black));
        mEntity.setStartDate(dateFormat);
        mEntity.setStartDateStamp(date.getTime());
        SimpleDateFormat sdf = new SimpleDateFormat("E");
        String week = sdf.format(date);
        if (week.contains("周"))
            week = week.replace("周", "");
        mEntity.setStartWeek(week);
        if (mEntity.getReturnDateStamp() != 0 && date.getTime() > mEntity.getReturnDateStamp()) {
            mEntity.setReturnDateStamp(0);
            mEntity.setReturnDate("");
            binding.tvChooseDateArrive.setText("返回日期");
            binding.tvChooseDateArrive.setTextColor(getResources().getColor(R.color.color_C0C4CC));
        }
    }

    /**
     * 填充返回日期
     *
     * @param date
     */
    public void setReturnDate(Date date) {
        String dateFormat = DateUtils.timestampToDate(date.getTime());
        binding.tvChooseDateArrive.setText(dateFormat);
        binding.tvChooseDateArrive.setTextColor(getResources().getColor(R.color.black));
        mEntity.setReturnDate(dateFormat);
        mEntity.setReturnDateStamp(date.getTime());
        SimpleDateFormat sdf = new SimpleDateFormat("E");
        String week = sdf.format(date);
        if (week.contains("周"))
            week = week.replace("周", "");
        mEntity.setBackWeek(week);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_choose_city_start) {//出发城市、
            if (onClickCallBack != null) {
                onClickCallBack.chooseStartCity();
            }
        } else if (id == R.id.tv_choose_city_arrive) {//到达城市、
            if (onClickCallBack != null) {
                onClickCallBack.chooseArriveCity();
            }
        } else if (id == R.id.tv_choose_date_start) {//出发日期、
            if (onClickCallBack != null) {
                onClickCallBack.chooseStartDate();
            }
        } else if (id == R.id.tv_choose_date_arrive) {//返回日期、
            if (onClickCallBack != null) {
                onClickCallBack.chooseReturnDate();
            }
        } else if (id == R.id.tv_search_flight) {//搜索
            if (onSearchCallBack != null) {
                onSearchCallBack.search(mEntity);
            }
        } else if (id == R.id.iv_change_city) {//交换城市
            if (mEntity == null || TextUtils.isEmpty(mEntity.getStartCity()) || TextUtils.isEmpty(mEntity.getArriveCity())) {
                MToast.showToast(getContext(), "请完善城市信息");
                return;
            }
            //创建旋转动画
            AnimUtils.rotateArrow(binding.ivChangeCity, false);

            binding.tvChooseCityStart.setText(mEntity.getArriveCity());
            binding.tvChooseCityArrive.setText(mEntity.getStartCity());
            mEntity.setStartCity(binding.tvChooseCityStart.getText().toString());
            mEntity.setArriveCity(binding.tvChooseCityArrive.getText().toString());
            String startCityNo = mEntity.getStartCityNo();
            mEntity.setStartCityNo(mEntity.getArriveCityNo());
            mEntity.setArriveCityNo(startCityNo);
        }
    }

    public void setOnClickCallBack(OnClickCallBack onClickCallBack) {
        this.onClickCallBack = onClickCallBack;
    }

    public interface OnClickCallBack {
        void chooseStartCity();

        void chooseArriveCity();

        void chooseStartDate();

        void chooseReturnDate();

    }

    public void setOnSearchCallBack(OnSearchCallBack onSearchCallBack) {
        this.onSearchCallBack = onSearchCallBack;
    }

    public interface OnSearchCallBack {
        void search(FlightSearchEntity entity);

    }
}
